#!/usr/bin/perl -w

package Process;

sub new
{
    my $class = shift;
    my $self={
    	_startTime	=>	undef,
    	_bursts		=>	[],
    	_name		=>	undef,
    	_scheduler	=>	undef,
    	_quantumLeft => 0,
    	_ults		=> 	[],
    	_ultsNew    =>  [],
    	_priority   =>  0,
    	
    };
         
    bless $self, $class;
    return $self;
}
sub setStartTime {
    my ( $self, $startTime ) = @_;
    $self->{_startTime} = $startTime if defined($startTime);
}
sub setBursts {
    my ( $self, $bursts ) = @_;
    $self->{_bursts} = $bursts;
}
sub setBurstsUlt{
		my ( $self, $name , @bursts ) = @_;
		
		my $ultsLenght = @{$self->{_ults}};
		my $b;
		
		for($b=0;$b<$ultsLenght;$b=$b+1){
			my $ultName = $self->{_ults}[$b]->getName();
				
			if($ultName eq $name){
				$self->{_ults}[$b]->{_burst} = @bursts;
			}
		}

}

sub setUltBlocked{
	$self->{_ultBloqued} = 1;
}

sub setUltUnblocked{
	$self->{_ultBloqued} = 0;
}


sub setName {
    my ( $self, $name ) = @_;
    $self->{_name} = $name if defined($name);
}
sub getStartTime {
	my( $self ) = @_;
    return $self->{_startTime};
}
sub getBursts {
	my( $self ) = @_;
    return $self->{_bursts};
}
sub getName {
	my( $self ) = @_;
    return $self->{_name};
}
sub getScheduler{
	my($self) = @_;
	return $self->{_scheduler};
}
sub setScheduler{
	my( $self, $scheduler ) = @_;
	$self->{_scheduler}=$scheduler;
}
sub hasUlts{
	my( $self ) = @_;
	$ultLength = @{$self->{_ults}};
	return $ultLength > 0;
}

sub hasUltsNew{
	my( $self ) = @_;
	$ultLength = @{$self->{_ultsNew}};
	return $ultLength > 0;
}

sub getUlts{
	my( $self ) = @_;
	return $self->{_ults};
}

sub getUltsNew{
	my( $self ) = @_;
	return $self->{_ultsNew};
}

sub setQuantumLeft{
	my( $self,$quantum ) = @_;
	return $self->{_quantumLeft} = $quantum;
}
sub getQuantumLeft{
	my( $self ) = @_;
	return $self->{_quantumLeft};
}
#Ordena la cola de ults.
sub orderUltByMinor{
	my( $self ) = @_;
	
	my $ultLenght= @{$self->{_ults}};
	my $aux;
	
	if($ultLength){
		  for(my $i=$ultLenght-1;$i>0;$i--){
				if($self->{_ults}[$i]->{_bursts}[0]->getValue() < $self->{_ults}[$i-1]->{_bursts}[0]->getValue()){
					$aux = $self->{_ults}[$i-1];
					$self->{_ults}[$i-1] = $self->{_ults}[$i];
					$self->{_ults}[$i] = $aux;
				}	
			}
	}
}

sub orderUltByPriorityHRRN{
	my( $self, $ticks ) = @_;
	my $proc;
	
	foreach $proc (@{$self->{_ults}}){
		$proc->{_priority} = ($ticks - $proc->{_startTime} + $proc->{_bursts}[0]->getValue())/$proc->{_bursts}[0]->getValue();	
	}
	
	
	my $ultLenght= @{$self->{_ults}};
	my $aux;
	
	if($ultLength){
		  for(my $i=$ultLenght-1;$i>0;$i--){
				if($self->{_ults}[$i]->{_priority} >= $self->{_ults}[$i-1]->{_priority}){
					$aux = $self->{_ults}[$i-1];
					$self->{_ults}[$i-1] = $self->{_ults}[$i];
					$self->{_ults}[$i] = $aux;
				}	
			}
	}
}


sub getNewsUlts{
	my( $self, $ticks ) = @_;
	
	my @lst = @{$self->{_ultsNew}};
	my @ind;
	my $proc;
	my $i=0;

	foreach $proc (@lst){
		if($proc->{_startTime} <= $ticks){
			push @{$self->{_ults}}, $proc;
			push @ind, $i;
		
			$i++;
		}
	}
	
	for(my $a=0;$a<$i;$a++){
		shift @{$self->{_ultsNew}}		
	}
	
	
	
}


1;